package pe.edu.upc.dai.grupo4.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dai.grupo4.model.Tabla;
import pe.edu.upc.dai.grupo4.utils.ConnectionUtils;
import pe.edu.upc.dai.grupo4.utils.DaoException;

/**
 *
 * @author Tito Luyo Murata
 */
public class TablaDaoImpl implements TablaDao {

    public List<Tabla> selectPorTabla(String Tabla) {
        List<Tabla> result = new ArrayList<Tabla>();
        Connection connection = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.prepareStatement("{call dbo.usp_Columnas_SEL_x_Tabla(?)}");
            st.setString(1, Tabla);
            rs = st.executeQuery();
            while (rs.next()) {
                result.add(this.getData(rs));
            }
        } catch (SQLException e) {
            throw new DaoException(e);
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return result;
    }

    private Tabla getData(ResultSet rs) throws SQLException {
        Tabla result = null;
        result = new Tabla(
                rs.getString("Nombre"),
                rs.getString("Tipo"));
        return result;
    }
}
